Public Class frmTemplateEditor

  Private mStrText As String
  Private mStrFileName As String
  Private mObjCtrlKey As Boolean = False

  Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
    Dim lObjOpenFile As New OpenFileDialog
    With lObjOpenFile
      .Title = "Open File"
      .Filter = "Template File|*.tmpl|XML File|*.xml|All File|*.*"
      .Multiselect = False
      If .ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
      mStrFileName = .FileName
      txtEditor.LoadFile(mStrFileName)
      Me.Text = "Template Editor - " & mStrFileName
      mStrText = String.Copy(txtEditor.Text)
    End With
  End Sub

  Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
    If txtEditor.Text <> mStrText Then
      If MsgBox("Do you want save file ?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, gCONST_MSG_HEADER) = MsgBoxResult.Yes Then
        SaveFile()
      End If
    End If
    Me.Close()
  End Sub

  Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    If mStrFileName <> "" Then
      txtEditor.SaveFile(mStrFileName)
      mStrText = String.Copy(txtEditor.Text)
    Else
      SaveFile()
    End If
  End Sub

  Private Sub cmdXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXML.Click
    Me.txtEditor.Document.HighlightingStrategy = ICSharpCode.TextEditor.Document.HighlightingStrategyFactory.CreateHighlightingStrategy("XML")
  End Sub

  Private Sub cmdNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNormal.Click
    Me.txtEditor.Document.HighlightingStrategy = ICSharpCode.TextEditor.Document.HighlightingStrategyFactory.CreateHighlightingStrategy("")
  End Sub

  Private Sub cmdSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveAs.Click
    SaveFile()
  End Sub

  Private Sub frmTemplateEditor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.Control Then
      mObjCtrlKey = True
    End If
    If mObjCtrlKey = True Then
      If e.KeyCode = Keys.S Then
        If mStrFileName <> "" Then
          txtEditor.SaveFile(mStrFileName)
          mStrText = String.Copy(txtEditor.Text)
        End If
      End If
      mObjCtrlKey = False
    End If
  End Sub

  Private Sub lstInsert_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstInsert.MouseDoubleClick
    txtEditor.ActiveTextAreaControl.TextArea.InsertString(lstInsert.Text.Split("|")(1).Trim)
  End Sub


  Private Sub SaveFile()
    Dim lObjSaveFile As New SaveFileDialog

    With lObjSaveFile
      .Title = "Save File"
      If mStrFileName <> "" Then
        .InitialDirectory = New IO.FileInfo(mStrFileName).Directory.Name
        .FileName = New IO.FileInfo(mStrFileName).Name
      End If
      .Filter = "Template File|*.tmpl|XML File|*.xml|All File|*.*"
      If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
      mStrFileName = .FileName
      txtEditor.SaveFile(mStrFileName)
      Me.Text = "Template Editor - " & mStrFileName
      mStrText = String.Copy(txtEditor.Text)
    End With

  End Sub

End Class